package icedot.library.common.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import icedot.library.common.R;
import icedot.library.common.base.BaseActivity;
import icedot.library.common.utils.Md5;
import icedot.library.common.utils.StringUtils;

/**
 * Created by IceDot on 2016/3/12.
 */
public class WindowProgressDlg extends PopupWindow
{
    private BaseActivity _rootActivity;
    private View         _menuView;

    private boolean _canCancel = true;
    private boolean _showProgress = true;
    private String _content = "";

    private View         _view_black;
    private View         _view_black2;
    private View         _view_black3;
    private View         _view_black4;
    private TextView     _text_content;

    private View.OnClickListener _listenerClose = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(_canCancel)
            {
                dismiss();
            }
        }
    };

    private WindowProgressDlg(BaseActivity act, String text, boolean progress,boolean canCancel)
    {
        _rootActivity = act;
        _canCancel = canCancel;
        _showProgress = progress;
        if(StringUtils.isNullOrEmpty(text))
        {
            _content = "";
        }
        else
        {
            _content =  text;
        }

        LayoutInflater inflater = (LayoutInflater) _rootActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        _menuView = inflater.inflate(R.layout.popwindow_progress, null);
        //设置SelectPicPopupWindow的View
        this.setContentView(_menuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x0d000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

        initView(_menuView);
        initData();
    }

    private void initView(View v)
    {
        _view_black = v.findViewById(R.id.view_black);
        _view_black2 = v.findViewById(R.id.view_black2);
        _view_black3 = v.findViewById(R.id.view_black3);
        _view_black4 = v.findViewById(R.id.view_black4);
        if(_showProgress)
        {
            v.findViewById(R.id.dlgprogress_progressbar).setVisibility(View.VISIBLE);
        }
        else
        {
            v.findViewById(R.id.dlgprogress_progressbar).setVisibility(View.GONE);
        }
        _text_content = (TextView)v.findViewById(R.id.dlgprogress_text_content);
        _text_content.setText(_content);

        _view_black.setOnClickListener(_listenerClose);
        _view_black2.setOnClickListener(_listenerClose);
        _view_black3.setOnClickListener(_listenerClose);
        _view_black4.setOnClickListener(_listenerClose);

        _menuView.setOnKeyListener(new View.OnKeyListener()
        {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if (keyCode == KeyEvent.KEYCODE_BACK && !_canCancel)
                {
                    return true;
                }
                return false;
            }
        });
    }

    public void updateContent(String content)
    {
        if(_text_content != null)
        {
            _text_content.setText(content);
        }
    }

    private void initData()
    {

    }

    public void setCancelable(boolean cancelable)
    {
        _canCancel = cancelable;
    }

    public static WindowProgressDlg showDlg(final View v,BaseActivity act,String content,
                               boolean showProgressBar,boolean enableCancel)
    {
        final WindowProgressDlg popup = new WindowProgressDlg(act,content,showProgressBar,enableCancel);
        v.post(new Runnable()
        {
            @Override
            public void run()
            {
                popup.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });

        return popup;
    }
}
